package com.blb.service.impl;

import com.blb.entity.*;
import com.blb.mapper.*;
import com.blb.service.IMenuService;
import com.blb.service.IUserRoleService;
import com.blb.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blb.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tb
 * @since 2020-05-20
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private UserMapper  userMapper;
    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private RoleMenuMapper roleMenuMapper;
    @Resource
    private RoleMapper roleMapper;
    @Autowired
    private IMenuService menuService;
    /**
     * 用户登录
     * @param account
     * @return
     */
    @Override
    public User login(String account) {
        System.out.println(account);
        return userMapper.login(account);
    }

    /**
     * 用户注册
     * @param company
     * @param user
     */
    @Transactional
    @Override
    public Integer userRegiest(Company company, User user) {
        companyMapper.insert(company);
        userMapper.insert(user);
        UserRole userRole = new UserRole();
        //默认给企业一个管理员的权限
        userRole.setId(UUIDUtils.getId());
        userRole.setUserId(user.getId());
        userRole.setRoleId("1");
        userRoleMapper.insert(userRole);

        return 1;

    }

    @Override
    public Set<String> getRoles(String userAccount) {
        return userMapper.getRoles(userAccount);
    }

    @Override
    public Set<String> getPermision(Set<String> roles) {
        return userMapper.getPermision(roles);
    }
}
